<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\getdatas\MySubject;
use app\admin\builder\Builder;
use app\BaseController;
use think\Exception;
use think\facade\Db;
use think\Request;

class Huisou extends BaseController
{
    public function show_list(Request $request){
        return Builder::table()
            ->setAction('huisou/page')
            ->addToolbarDemo('alldel','全部删除','','huisou/qudel')
            ->addToolbarDemo('alldel','全部恢复','','huisou/quhuifu','','background-color: #00b7ee')
            ->setColumn('input','name','名称')
            ->setColumn('input','cid_name','栏目')
            ->setColumn('input','model','表id')
            ->setRightHeader()
            ->setButton('del','恢复','huisou/huifu','','background-color: #00b7ee')
            ->setButton('del','删除','huisou/del')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $page = $request->param('page',1);
        $model = Db::name(getTable('models'))
            ->where('type','单表')
            ->select()->toArray();
        $data = [];
//        echo '<pre>';
//        print_r($model);
//        exit;
        foreach ($model as $value){
            $array = Db::name($value['table'])
                ->where('delete_time','<>',0)
                ->select()->toArray();
            if($array){
                $data = array_merge($data,$array);
            }
        }
        $model_lianbiao = Db::name(getTable('models'))
            ->where('type','联表')
            ->select()->toArray();
//        echo '<pre>';
//        print_r($model_lianbiao);
//        exit;
        $lianbial_data = [];
        foreach ($model_lianbiao as $value){
            $array = Db::name($value['table'])
                ->where('delete_time','<>',0)
                ->cursor();
            if($array){
                foreach ($array as $values){
                    $newarray = Db::name($value['table'])->where('id',$values['id'])
                        ->find();
                    if($newarray){
                        $newarray['model'] = $value['id'];
                        $newarray['cid'] = '';
                        $lianbial_data = array_merge($lianbial_data,[$newarray]);
                    }
                }
            }
        }
        $data = array_merge($data,$lianbial_data);

//        echo '<pre>';
//        print_r($data);
//        exit;
        $data = array_slice($data,($page-1)*$limit,$limit);
        foreach ($data as $key=>$value){
            try{
                $cid = Db::name(getTable('column'))->where('id',$value['cid'])
                    ->find();
                if($cid){
                    $value['cid_name'] = $cid['name'];
                    $model = Db::name(getTable('models'))->where('id',$cid['model_id'])->find();
                    if($model){
                        $value['model'] = $model['id'];
                    }
                }
            }catch (Exception $e){
                $value['cid'] = '';
            }
            $data[$key] = $value;
        }
//        echo '<pre>';
//        print_r($data);
//        exit;
        $res['total'] = count($data);
        $res['data'] = $data;
//        $res = Db::name('index_danbiao')
//            ->where('delete_time',1)
//            ->paginate(10)
//            ->toArray();
        return json_page_response($res);
    }
    public function huifu(Request $request){
        $data  = $request->param('data');
        $obj = new MySubject();
//        $obj->setCid($data['cid']);
        $obj->setModelId($data['model']);
        $table = $obj->getModel('table');
        if(Db::name($table)->where('id',$data['id'])->update(['delete_time'=>0])){
            return $this->success('恢复成功');
        }else{
            return $this->error('恢复失败');
        }
    }
    public function del(Request $request){
        $data  = $request->param('data');
        $obj = new MySubject();
//        $obj->setCid($data['cid']);
        $obj->setModelId($data['model']);
        $table = $obj->getModel('table');
        if(Db::name($table)->where('id',$data['id'])->delete()){
            return $this->success('删除成功');
        }else{
            return $this->success('删除失败');
        }
    }
    public function quhuifu(Request $request){
        $data = $request->param('data');
//        return $this->success('删除成功',$data);
        $obj = new MySubject();
        foreach ($data as $value){
//            $obj->setCid($value['cid']);
            $obj->setModelId($value['model']);
            $table = $obj->getModel('table');
            if($table){
                Db::name($table)->where('id',$value['id'])->update(['delete_time'=>0]);
            }
        }
        return $this->success('恢复成功');
    }
    public function qudel(Request $request){
        $data = $request->param('data');
//        return $this->success('删除成功',$data);
        $obj = new MySubject();
        foreach ($data as $value){
//            $obj->setCid($value['cid']);
            $obj->setModelId($value['model']);
            $table = $obj->getModel('table');
            if($table){
                Db::name($table)->where('id',$value['id'])->delete();
            }
        }
        return $this->success('删除成功');
    }
}